package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.zhgd.domain.bo.NAsphaltAlarmHandlerEditBo;
import com.ruoyi.zhgd.domain.vo.NAsphaltAlarmCountVo;
import com.ruoyi.zhgd.domain.vo.NAsphaltAlarmHandlerVo;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.NAsphaltAlarmBo;
import com.ruoyi.zhgd.domain.vo.NAsphaltAlarmVo;
import com.ruoyi.zhgd.domain.NAsphaltAlarm;
import com.ruoyi.zhgd.mapper.NAsphaltAlarmMapper;
import com.ruoyi.zhgd.service.INAsphaltAlarmService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 沥青拌合站筛孔数据Service业务层处理
 *
 * @author zyb
 * @date 2023-09-06
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class NAsphaltAlarmServiceImpl implements INAsphaltAlarmService {

    private final NAsphaltAlarmMapper baseMapper;

    /**
     * 查询沥青拌合站筛孔数据
     */
    @Override
    public NAsphaltAlarmVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询沥青拌合站筛孔数据列表
     */
    @Override
    public TableDataInfo<NAsphaltAlarmVo> queryPageList(NAsphaltAlarmBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NAsphaltAlarm> lqw = buildQueryWrapper(bo);
        Page<NAsphaltAlarmVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询沥青拌合站筛孔数据列表
     */
    @Override
    public List<NAsphaltAlarmVo> queryList(NAsphaltAlarmBo bo) {
        LambdaQueryWrapper<NAsphaltAlarm> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NAsphaltAlarm> buildQueryWrapper(NAsphaltAlarmBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NAsphaltAlarm> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getSieve0d075() != null, NAsphaltAlarm::getSieve0d075, bo.getSieve0d075());
        lqw.eq(bo.getSieve0d15() != null, NAsphaltAlarm::getSieve0d15, bo.getSieve0d15());
        lqw.eq(bo.getSieve0d3() != null, NAsphaltAlarm::getSieve0d3, bo.getSieve0d3());
        lqw.eq(bo.getSieve0d6() != null, NAsphaltAlarm::getSieve0d6, bo.getSieve0d6());
        lqw.eq(bo.getSieve1d18() != null, NAsphaltAlarm::getSieve1d18, bo.getSieve1d18());
        lqw.eq(bo.getSieve2d36() != null, NAsphaltAlarm::getSieve2d36, bo.getSieve2d36());
        lqw.eq(bo.getSieve4d75() != null, NAsphaltAlarm::getSieve4d75, bo.getSieve4d75());
        lqw.eq(bo.getSieve9d5() != null, NAsphaltAlarm::getSieve9d5, bo.getSieve9d5());
        lqw.eq(bo.getSieve13d2() != null, NAsphaltAlarm::getSieve13d2, bo.getSieve13d2());
        lqw.eq(bo.getSieve16() != null, NAsphaltAlarm::getSieve16, bo.getSieve16());
        lqw.eq(bo.getSieve19() != null, NAsphaltAlarm::getSieve19, bo.getSieve19());
        lqw.eq(bo.getSieve26d5() != null, NAsphaltAlarm::getSieve26d5, bo.getSieve26d5());
        lqw.eq(bo.getSieve31d5() != null, NAsphaltAlarm::getSieve31d5, bo.getSieve31d5());
        lqw.eq(bo.getSieve37d5() != null, NAsphaltAlarm::getSieve37d5, bo.getSieve37d5());
        lqw.eq(bo.getSieve52() != null, NAsphaltAlarm::getSieve52, bo.getSieve52());
        lqw.eq(bo.getSieve53() != null, NAsphaltAlarm::getSieve53, bo.getSieve53());
        lqw.eq(bo.getSieve() != null, NAsphaltAlarm::getSieve, bo.getSieve());
        lqw.eq(bo.getTemperature() != null, NAsphaltAlarm::getTemperature, bo.getTemperature());
        lqw.eq(bo.getAsphaltAggregateRatio() != null, NAsphaltAlarm::getAsphaltAggregateRatio, bo.getAsphaltAggregateRatio());
        lqw.eq(bo.getAlarm() != null, NAsphaltAlarm::getAlarm, bo.getAlarm());
        lqw.eq(StringUtils.isNotBlank(bo.getDetails()), NAsphaltAlarm::getDetails, bo.getDetails());
        lqw.eq(bo.getState() != null, NAsphaltAlarm::getState, bo.getState());
        lqw.eq(StringUtils.isNotBlank(bo.getAlarmReason()), NAsphaltAlarm::getAlarmReason, bo.getAlarmReason());
        lqw.eq(StringUtils.isNotBlank(bo.getHandlerPlan()), NAsphaltAlarm::getHandlerPlan, bo.getHandlerPlan());
        lqw.eq(StringUtils.isNotBlank(bo.getHandlerResult()), NAsphaltAlarm::getHandlerResult, bo.getHandlerResult());
        lqw.eq(bo.getHandlerTime() != null, NAsphaltAlarm::getHandlerTime, bo.getHandlerTime());
        lqw.eq(StringUtils.isNotBlank(bo.getHandlerPerson()), NAsphaltAlarm::getHandlerPerson, bo.getHandlerPerson());
        return lqw;
    }

    /**
     * 新增沥青拌合站筛孔数据
     */
    @Override
    public Boolean insertByBo(NAsphaltAlarmBo bo) {
        NAsphaltAlarm add = BeanUtil.toBean(bo, NAsphaltAlarm.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改沥青拌合站筛孔数据
     */
    @Override
    public Boolean updateByBo(NAsphaltAlarmBo bo) {
        NAsphaltAlarm update = BeanUtil.toBean(bo, NAsphaltAlarm.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NAsphaltAlarm entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除沥青拌合站筛孔数据
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 获取沥青预警分页列表
     *
     * @param bo
     * @param pageQuery
     * @return
     */
    @Override
    public TableDataInfo<NAsphaltAlarmVo> page(NAsphaltAlarmBo bo, PageQuery pageQuery) {
        // 判断是否设置预警类型
        if (ObjectUtils.isEmpty(bo.getAlarmType())) {
            bo.setAlarmType("all");
        }
        return TableDataInfo.build(baseMapper.page(bo, pageQuery.build()));
    }

    /**
     * 统计预警信息
     * @param bo
     * @return
     */
    @Override
    public NAsphaltAlarmCountVo count(NAsphaltAlarmBo bo) {
        return baseMapper.count(bo);
    }

    /**
     * 预警处理
     *
     * @param id
     * @return
     */
    @Override
    public NAsphaltAlarmHandlerVo handler(String id) {
        return baseMapper.handler(id);
    }

    /**
     * 预警处理
     *
     * @param bo
     */
    @Override
    public void handlerEdit(NAsphaltAlarmHandlerEditBo bo) {
        if (ObjectUtils.isEmpty(bo.getId())) {
            throw new ServiceException("主键id不能为空");
        }
        NAsphaltAlarm alarm = baseMapper.selectById(bo.getId());
        alarm.setAlarmReason(bo.getAlarmReason());
        alarm.setHandlerPlan(bo.getHandlerPlan());
        alarm.setHandlerResult(bo.getHandlerResult());
        alarm.setHandlerTime(bo.getHandlerTime());
        alarm.setHandlerPerson(bo.getHandlerPerson());
        alarm.setState(1L);
        baseMapper.updateById(alarm);
    }
}
